package buzzword;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import json.JSONArray;
import json.JSONException;
import json.JSONObject;

public class Client {

	private final String playerName;
	private int gameID = -1;
	private Board gameBoard;


	public Client(String playerName) {
		this.playerName = playerName;
		this.gameBoard = new Board();

	}


	public String get() throws JSONException {
		final String urlExtension = "get?gameid=" + gameID + "&player="
				+ playerName;
		JSONObject getter = new JSONObject(getJSON(urlExtension));
		return gameBoard.get(gameID, playerName, getter);
		

	}


	public boolean startGame() throws NoGameHostException, JSONException {
		final String urlExtension = "start?gameid=" + gameID + "&player="
				+ playerName;
		JSONObject starterMsg = new JSONObject(getJSON(urlExtension));
		if (starterMsg.getString("status").equals("error"))
			throw new NoGameHostException();
		else
			return true;
	}


	public boolean bingo() throws JSONException {
		final String urlExtension = "bingo?gameid=" + gameID + "&player="
				+ playerName;
		return new JSONObject(getJSON(urlExtension)).getString("status")
				.equals("won");

	}


	public void createGame(int columns, int rows, String category) throws JSONException {
		final String urlExtension = "new?title=MyGame&player="
				+ playerName + "&category="+category +"&rows=" + rows + "&columns=" + columns;
		JSONObject currentGame = new JSONObject(getJSON(urlExtension));
		this.gameID = currentGame.getInt("gameid");
	}

	public List<String> getWordsInDB(String category) throws JSONException {
		final String urlExtension = "words?category=" + category;
		String JSONWords = getJSON(urlExtension);
		return new WordList().getWords(JSONWords);
	}

	public List<Game> getGames() throws JSONException {
		final String urlExtension = "list";
		List<Game> currentGames = new ArrayList<Game>();
		JSONObject games = new JSONObject(getJSON(urlExtension));
		JSONArray gameArray = games.getJSONArray("games");
		for (int i = 0; i < gameArray.length(); i++) {
			currentGames.add(new Game(gameArray.getJSONObject(i)));
		}

		return currentGames;
	}

	public String getPlayerName() {
		return this.playerName;
	}

	public int getGameID() {
		return this.gameID;
	}


	public Board getBoard(){
		return this.gameBoard;
	}



	private String getJSON(String urlExtension) {
		final String baseAdress = "http://4c0.de/bingo/service/";
		InputStream input = null;
		try {
			URL option = new URL(baseAdress + urlExtension);
			input = option.openStream();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return getURLString(input);

	}

	private String getURLString(InputStream input) {
		Scanner in = new Scanner(input);
		String content = "";
		while (in.hasNext()) {
			content += in.next();
		}
		return content;
	}

}
